﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections;

namespace ResourceBlender.Express {
    /// <summary>
    /// Allows selection of an item from a list.
    /// </summary>
    public partial class WinListBoxSelection : Window {
        public static readonly DependencyProperty ItemsProperty = DependencyProperty.Register("Items", typeof(IEnumerable<string>), typeof(WinListBoxSelection));
        /// <summary>
        /// Gets or sets the items from which to choose.
        /// </summary>
        /// <value>The item from which to chooses.</value>
        public IEnumerable<string> Items {
            get { return (IEnumerable<string>)base.GetValue(ItemsProperty); }
            set { base.SetValue(ItemsProperty, value); }
        }

        /// <summary>
        /// Gets the selected item.
        /// </summary>
        /// <value>The selected item.</value>
        public string SelectedItem {
            get { return lstItems.SelectedItem.ToString(); }
        }

        /// <summary>
        /// Initializes a new <see cref="WinListBoxSelection"/>.
        /// </summary>
        public WinListBoxSelection() {
            InitializeComponent();
        }

        /// <summary>
        /// Initializes a new <see cref="WinListBoxSelection"/>.
        /// </summary>
        /// <param name="title">The title.</param>
        /// <param name="message">The message.</param>
        /// <param name="items">The items.</param>
        public WinListBoxSelection(string title, string message, IEnumerable<string> items) 
            :this() {
            this.Title = title;
            txtMessage.Text = message;
            this.Items = items;
        }

        /// <summary>
        /// Seletcs an item and closes the window on double click.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Input.MouseEventArgs"/> instance containing the event data.</param>
        private void lstItems_MouseDoubleClick(object sender, MouseEventArgs e) {
            if(lstItems.SelectedItem != null)
                DialogResult = true;
        }

        /// <summary>
        /// Determines whether a choice has been made.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.Input.CanExecuteRoutedEventArgs"/> instance containing the event data.</param>
        private void CanExecuteSave(object sender, CanExecuteRoutedEventArgs e) {
            e.CanExecute = this.IsLoaded && lstItems.SelectedItem != null;
        }

        /// <summary>
        /// Returns a DialogResult of true and closes the Window.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void Save(object sender, ExecutedRoutedEventArgs e) {
            DialogResult = true;
        }

        /// <summary>
        /// Closes the dialog without saving.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void CloseWindow(object sender, ExecutedRoutedEventArgs e) {
            DialogResult = false;
        }
    }
}
